<?php
/**
 * @version			$Id$
 * @author			Richard Asp
 * @package			Joomla
 * @subpackage		gnudoor
 * @copyright		(C) 2010 gnudoor LLC
 * @license      GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.view');

class GnudoorViewAppt extends JView 
{

	/**
	 * 
	 * @param unknown_type $tpl
	 */
	function display($tpl = null) {

		$host_articles_base = JRoute::_(JURI::base() . 'index2.php?option=com_content&view=article&id=');		
    	$model =& $this->getModel('Appt');
    	/* @var array.  Shows the time slots that are 
    	 * available for reservation */
    	$slots = $model->getSlots();
        /* @var String gets the current displayed Sunday
         * date of the week shown */
        $vardate = JRequest::getVar('fd');

        /*
         * Lookup the start date of the week.  returns the
         * date of Sunday of this week
         */
        if (isset($vardate)) {
        	$firstday = $model->getStartDate('wday', $vardate);
        } else {
        	$firstday = $model->getStartDate();
        }

        /*
         * See if the PHP function used in this section
         * is available.  If not, use the work around.
         */
        if (function_exists('date_create')) {
	        /*
	         * From here we need php 5.2 or greater
	         */
	        $holdfday = clone $firstday;  
	        $firstdateformated = date_format($holdfday,'Y-m-d');      
	
	        $dates = date_parse(date_format($firstday,DATE_RFC822));
			$fday = gmmktime($dates['hour'],$dates['minute'],$dates['second'],$dates['month'],$dates['day'],$dates['year']);
			$arr = getdate($fday);
	        $jd = $arr['yday'];
			
			$currday = date_create();
	        $todaydates = date_parse(date_format($currday,DATE_RFC822));
			$tday = gmmktime($todaydates['hour'],$todaydates['minute'],$todaydates['second'],$todaydates['month'],$todaydates['day'],$todaydates['year']);
			$tarr = getdate($tday);
	        $tjd = $tarr['yday'];
	                
	        /**
	         *  @var Array create the date headers for this week
	         */
	        $weekdates = array (
	        	date_format($firstday,'M d'),
	        	date_format($firstday->modify('+1 day'),'M d'),
	        	date_format($firstday->modify('+1 day'),'M d'),
	        	date_format($firstday->modify('+1 day'),'M d'),
	        	date_format($firstday->modify('+1 day'),'M d'),
	        	date_format($firstday->modify('+1 day'),'M d'),
	        	date_format($firstday->modify('+1 day'),'M d')
	        	);      
        } else {
	        /*
	         * Use this code if less than php 5.2
	         */
        	
			$firstdateformated = strftime('%Y-%m-%d',$firstday);
			$arr = getdate($firstday);
	        $jd = $arr['yday'];
			$currday = getdate();
	        $tjd = $currday['yday'];
	        
	        /**
	         *  @var Array create the date headers for this week
	         */
	        $weekdates = array();
	        for ( $i=0; $i < 7; $i++ ) {
	        	$weekdates[] = strftime('%b %d',$firstday);
	        	$firstday += 86400;	 
	        }        
        }
        
        /*
         * This section allows the user to go fowared or
         * back on the calendar.
         */
        $baseURI = JURI::base();
        $componentLink = JRoute::_($baseURI . 'index.php?option=com_gnudoor&controller=appt&task=change_test');
		$componentLink .= '&amp;fdate=' . $firstdateformated;		
		$reservationLink = JRoute::_($baseURI . 'index.php?option=com_gnudoor&controller=appt&task=create');
        $leftlink = '<a href="' . $componentLink . '&amp;direction=left' . '"> << </a>';
		$rightlink = '<a href="' . $componentLink . '&amp;direction=right' . '"> >> </a>';

        $days = array();
		// This displays the slots on the left
        for ( $i=0; $i < count($slots); $i++) {
        	$arr2 = array();
			//  This displays the week days on the top
        	for ( $d = 0 ; $d < 7; $d++ ) {
        		$jdate = $d + $jd;
				if ($tjd > $jdate) {
					// marks the day as closed if in the past
					$arr2[] = JText::_('closed');
				} else {
					// creates a link if the slot is open
					//  TODO:  need to mark it as busy if 
					//         slot is reserved.
					
	        		$arr2[] = '<a href="'
	        				. $reservationLink
	        				. '&amp;rdate='
	        				. $jdate
	        				. '&amp;rslot='
	        				. $i
	        				. '"><div>'
	        				. JText::_('free')
	        				. '</div></a>'
	        				;
				}
        	}
        	$days[] = $arr2;
        }

        /*
         * Assign the references to be displayed
         */
        $this->assignRef( 'slot', 			$slots );
		$this->assignRef( 'articlebase', 	$host_articles_base);
		$this->assignRef( 'right', 			$rightlink );
        $this->assignRef( 'left', 			$leftlink );
		$this->assignRef( 'day', 			$days );
		$this->assignRef( 'wname', 			$weekdates );
		
        parent::display($tpl);
    }

	function _tzlookup($ustz = 'EST') {
				    /*
		     *  Lookup the time zone names worldwide. 
		     */    
		$tzarray = array(
				'EST' => 'New_York',
				'EDT' => 'New_York',
				'CST' => 'Chicago',
				'CDT' => 'Chicago',
				'MST' => 'Denver',
				'MDT' => 'Denver',
				'PST' => 'Los_Angeles',
				'PDT' => 'Los_Angeles'
				);
				        
		$zones = timezone_identifiers_list();		       
		foreach ($zones as $zone)
		{
		    $zone = explode('/', $zone); // 0 => Continent, 1 => City
		    // Only use "friendly" continent names
		    if ($zone[0] == 'Africa' || $zone[0] == 'America' || $zone[0] == 'Antarctica' || $zone[0] == 'Arctic' || $zone[0] == 'Asia' || $zone[0] == 'Atlantic' || $zone[0] == 'Australia' || $zone[0] == 'Europe' || $zone[0] == 'Indian' || $zone[0] == 'Pacific')
		    {       
		        if ($zone[1] == $tzarray[$ustz])
		        {
					$tzname = $zone[0] . '/' . $zone[1];
		        }
		    }
		}

		return($tzname); 
	}

}


?>